import { defineStore } from 'pinia';
import { store } from '/@/store';
import type { AppRouteRecordRaw } from '/@/router/types';

export interface RouteState {
  cacheRouteList: Set<string>;
}

export const useRouteStore = defineStore({
  id: 'app_route',
  state: (): RouteState => ({
    cacheRouteList: new Set(),
  }),
  getters: {
    getCacheRouteList(): string[] {
      return Array.from(this.cacheRouteList);
    },
  },
  actions: {
    // 设置缓存页面
    setCacheRouteList(allRoutes: AppRouteRecordRaw[]) {
      const cacheMap: Set<string> = new Set();
      for (const tab of allRoutes) {
        if (tab.meta.keepAlive) {
          cacheMap.add(tab.name);
        }
      }
      this.cacheRouteList = cacheMap;
    },
  },
});

// Need to be used outside the setup
export function useRouteStoreWithOut() {
  return useRouteStore(store);
}
